<template>
  <div class="all">
    <van-nav-bar
      fixed
      safe-area-inset-top
      title="地图找房"
      left-arrow
      @click-left="back"
    />
    <div id="app">
      <div class="amap-page-container">
        <el-amap
          v-if="showmap"
          vid="amapDemo"
          :center="center"
          :zoom="zoom"
          class="amap-demo"
          pitch-enable="false"
        >
          <el-amap-marker
            v-for="(marker, index) in markers"
            :key="index"
            :events="marker.events"
            :position="marker.position"
          />
          <el-amap-info-window
            v-if="window"
            :position="window.position"
            :visible="window.visible"
            :content="window.content"
            :offset="window.offset"
            :close-when-click-map="true"
            :is-custom="true"
          >
            <div id="info-window">
              <img :src="window.image" />
              <div class="intrduce">
                <span class="title1">{{ window.address }}</span>
                <span class="title2">
                  <span>{{ window.houseType }}</span
                  >|<span
                    >{{ window.coveredArea }}m<span class="up">2</span></span
                  >|<span>{{ window.floor }}</span></span
                >
                <div class="tips">
                  <span>{{ window.aspectType }}</span>
                  <span>{{ window.decoration }}</span>
                  <span>{{ window.houseGenre }}</span>
                </div>
                <span class="title3">{{ window.listedPrice }}万</span>
              </div>
              <div class="detail" @click="checkDetail(window.houseId)">
                查看详情
              </div>
            </div>
          </el-amap-info-window>
        </el-amap>
      </div>
    </div>
  </div>
</template>
    
<script>
import { getBuyhouselist } from "../../apis/house.js";
// import { getDisarea } from "../../apis/map.js";
import local from "../../utils/local";
export default {
  data() {
    return {
      data: [],
      zoom: 10,
      center: [104.066301, 30.572961],
      markers: [],
      windows: [],
      window: "",
      currentPage: 1,
      perPage: 1000,
      showmap: false,
    };
  },
  created() {
    // getDisarea({
    //   key: this.$AMapkey,
    //   keywords: local.get("city"),
    //   subdistrict: 2,
    // }).then((res) => {
    //   this.center = res.data.districts[0].center.split(",");
    //   this.showmap = true;
    //   this.point();
    // });
  },
  mounted() {},
  methods: {
    back() {
      this.$router.go(-1);
    },
    point() {
      const markers = [];
      const windows = [];
      const that = this;
      getBuyhouselist({
        citycode: local.get("citycode"),
        currentPage: this.currentPage,
        perPage: this.perPage,
      }).then((response) => {
        JSON.parse(JSON.stringify(response));
        response.result.list.forEach((e) => {
          if (e.lat || e.lng) {
            let a = {};
            a.position = `${e.lng},${e.lat}`;
            a.address = e.subdistrict;
            a.image = e.houseImage;
            a.houseType = e.houseType;
            a.coveredArea = e.coveredArea;
            a.floor = e.floor;
            a.listedPrice = e.listedPrice;
            a.houseId = e.houseId;
            a.aspectType = e.aspectType;
            a.decoration = e.decoration;
            a.houseGenre = e.houseGenre;
            a.lng = e.lng;
            a.lat = e.lat;
            that.data.push(a);
            a = {};
          }
        });
        this.data = JSON.parse(JSON.stringify(this.data));
        this.data.forEach((e, index) => {
          markers.push({
            position: e.position.split(","),
            // icon:item.url, //不设置默认蓝色水滴
            events: {
              click() {
                // 方法：鼠标移动到点标记上，显示相应窗体
                that.windows.forEach((window) => {
                  window.visible = false; // 关闭窗体
                });
                that.window = that.windows[index];
                that.$nextTick(() => {
                  that.window.visible = true;
                });
              },
            },
          });
          windows.push({
            position: e.position.split(","),
            isCustom: true,
            offset: [115, 55], // 窗体偏移
            showShadow: false,
            visible: false, // 初始是否显示
            address: e.address,
            image: e.image,
            houseType: e.houseType,
            coveredArea: e.coveredArea,
            floor: e.floor,
            listedPrice: e.listedPrice,
            houseId: e.houseId,
            aspectType: e.aspectType,
            decoration: e.decoration,
            houseGenre: e.houseGenre,
            lng: e.lng,
            lat: e.lat,
          });
        });
      });
      //  加点
      this.markers = markers;
      // 加弹窗
      this.windows = windows;
    },
    checkDetail(e) {
      local.set("houseId", e);
      this.$router.push({ path: "/buyhousedetail" });
    },
  },
};
</script>
    
<style lang="less" scoped>
.all {
  height: 100%;
  display: flex;
  flex-direction: column;
  #app {
    flex: 1;
  }
}
.amap-page-container {
  height: 100%;
}
.amap-demo {
  height: 100%;
}

.amap-page-container {
  position: relative;
}

#info-window {
  padding: 5px;
  width: 230px;
  height: 120px;
  margin-left: 20px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 4px;
  position: relative;
  overflow: hidden;
  display: flex;
  border-bottom: 1px solid #f1f1f1;
  img {
    width: 70px;
    height: 70px;
  }
  .intrduce {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-left: 10px;
    width: 190px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    height: 90px;
    .title1 {
      font-size: 16px;
      font-weight: 550;
      color: #333333;
    }
    .title2 {
      font-size: 12px;
      color: #a0a0a0;
      span {
        margin: 0 2px;
      }
    }
    .tips {
      span {
        font-size: 10px;
        display: inline-block;
        margin-right: 5px;
        padding: 2px;
        background-color: #ebfafd;
        color: #4daac1;
      }
    }
    .title3 {
      color: #d43b33;
      font-size: 16px;
      font-weight: bold;
      span {
        color: #999;
        font-size: 12px;
        margin-left: 6px;
      }
    }
  }
  .up {
    margin: 0px !important;
    font-size: 12px;
    vertical-align: super;
    transform: scale(calc(10 / 12));
  }
}

.detail {
  width: 100%;
  box-sizing: border-box;
  height: 28px;
  line-height: 28px;
  color: #fff;
  background-color: #45b7cb;
  position: absolute;
  bottom: 0;
  left: 0;
  font-size: 14px;
  text-align: center;
  cursor: pointer;
}
</style>